#include <iostream>
#include <unistd.h>
using namespace std;
int main() {
    int result = -1;
    int fd[2],nbytes;
    pid_t pid;
    string str_info = "hello 管道";
    char readbuffer[80];
    int *read_fd = &fd[0];
    int *write_fd = &fd[1];
    result = pipe(fd);
    if(-1 == result){
        cout<<"创建管道失败"<<endl;
        return -1;
    }
    pid = fork();
    if(-1 == pid)
    {
        cout<<"fork 进程失败";
        return -1;
    }
    if( 0 == pid)
    {
        close(*read_fd);
        result = write(*write_fd,str_info.c_str(),str_info.size());
        return 0;
    }
    else{
        close(*write_fd);
        nbytes = read(*read_fd,readbuffer, sizeof(readbuffer));
        cout<<"接收到"<<nbytes<<"个数据，内容为:"<<readbuffer<<"\n";
        return 0;
    }
}